package com.lhdx.practical.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StrUtils {
    public static final String DEFAULT_STRING = "";
    public static final int DEFAULT_INT = 0;
    public static final long DEFAULT_LONG = 0l;
    public static final double DEFAULT_DOUBLE = 0.00;

    public static String getString(HttpServletRequest request, String s) {
        return getString(request, s, DEFAULT_STRING);
    }

    public static String getString(HttpServletRequest request, String s, String defaultString) {
        String s1 = "";
        try{
            s1 = request.getParameter(s);
            if(s1 != null && !"".equals(s1) && isMessyCode(s1)){
                s1 = new String(s1.getBytes("iso8859-1"),"UTF-8");
            }
            //s1 = new String(s1.getBytes("iso8859-1"),"UTF-8");
            if (StringUtils.isBlank(s1)){
                return defaultString;
            }
        }catch (Exception e) {
            s1 = defaultString;
        }
        return XssUtils.xssChange(s1);
    }

    public static int getInt(HttpServletRequest request, String s){
        return getInt(request, s, DEFAULT_INT);
    }

    public static int getInt(HttpServletRequest request, String s,
                             int defaultInt) {
        try {
            String temp = getString(request, s);
            if (StringUtils.isBlank(temp))
                return defaultInt;
            else
                return Integer.parseInt(temp);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    public static long getLong(HttpServletRequest request, String s){
        return getLong(request, s, DEFAULT_LONG);
    }

    public static long getLong(HttpServletRequest request, String s,
                             long defaultLong) {
        try {
            String temp = getString(request, s);
            if (StringUtils.isBlank(temp))
                return defaultLong;
            else
                return Long.parseLong(temp);
        } catch (NumberFormatException e) {
            return 0;
        }
    }


    /**
     * 判断字符串是否是乱码
     *
     * @param strName 字符串
     * @return 是否是乱码
     */
    public static boolean isMessyCode(String strName) {
        try {
            Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");
            Matcher m = p.matcher(strName);
            String after = m.replaceAll("");
            String temp = after.replaceAll("\\p{P}", "");
            char[] ch = temp.trim().toCharArray();

            int length = (ch != null) ? ch.length : 0;
            for (int i = 0; i < length; i++) {
                char c = ch[i];
                if (!Character.isLetterOrDigit(c)) {
                    String str = "" + ch[i];
                    if (!str.matches("[\u4e00-\u9fa5]+")) {
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
